<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-form label-width="60px">
          <el-button size="small" type="primary" @click="showAdded">新增面试技巧</el-button>
          <el-form-item label="关键字" style="width: 300px; margin-top: 15px">
            <el-input v-model="queryInfo.keyword" @clear='skillList()' @keyup.enter.native='skillList()' clearable>
              <el-button slot="append" icon="el-icon-search"  @click="skillList()"></el-button>
            </el-input>
          </el-form-item>
        </el-form>

        <el-table :data="list"  border stripe>
          <el-table-column type="index" label="序号" min-width="300px"></el-table-column>
          <el-table-column prop="title" label="标题" min-width="100px"></el-table-column>
          <el-table-column prop="visits" label="阅读数" min-width="50px"></el-table-column>
          <el-table-column prop="state" label="状态" min-width="60px">
            <template slot-scope="scope">
              <el-tag type="success" v-if="scope.row.state == 1">启用</el-tag>
              <el-tag type="danger" v-else>禁用</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="creator" label="录入人" min-width="100px"></el-table-column>
          <el-table-column label="操作" min-width="200px">
            <template slot-scope="scope">
               <!-- 修改按钮 -->
              <el-tooltip class="item" effect="dark" content="修改" placement="top">
                <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
              </el-tooltip>
               <!-- 删除按钮 -->
              <el-tooltip class="item" effect="dark" content="删除" placement="top">
                 <el-button type="danger" icon="el-icon-delete" size="mini"></el-button>
              </el-tooltip>
              <!-- 预览按钮 -->
              <el-tooltip class="item" effect="dark" content="预览" placement="top">
                <el-button type="info" icon="el-icon-view" size="mini"></el-button>
              </el-tooltip>
              <!-- 状态按钮 -->
              <el-tooltip class="item" effect="dark" content="状态切换" placement="top">
                 <el-button type="warning" icon="el-icon-setting" size="mini" @click="stateCut(scope.row)"></el-button>
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.page"
        :page-sizes="[1, 3, 5, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="counts"
        style="margin-top: 15px">
      </el-pagination>
      </el-card>

      <!-- 添加文章对话框 -->
      <el-dialog title="添加面试技巧" :visible.sync="dialogVisible" width="60%" :before-close="handleClose" @close="resetAddForm">
        <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="70px">
          <el-form-item label="标题：" prop="title">
            <el-input v-model="addForm.title"></el-input>
          </el-form-item>
          <el-form-item>
            <quill-editor style="margin-bottom: 15px;" v-model="addForm.articleBody"></quill-editor>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addArticle">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>

import { list, state, add } from '@/api/hmmm/articles.js'

export default {
  name: 'ArticlesList',
  created() {
    this.skillList()
  },
  data() {
    return {
      list: [],
      counts: 0,
      queryInfo: {
        page: 0,
        keyword: '',
        pagesize: 3,
      },
      dialogVisible: false,
      addForm: {
        title: '',
        articleBody: '',
        videoURL: ''
      },
      // 添加属性的验证规则
      addFormRules: {
        title: [
           { required: true, message: '请输入商品名称', trigger: 'blur' },
        ]
      },
    }
  },
  methods: {
    async skillList() {
      const { data: res } = await list(this.queryInfo)
      console.log(res);

      this.list = res.items
      this.pages = res.pages
      this.counts = res.counts
      // console.log(res);
    },
     // 监听每页显示几条事件
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.skillList()
    },
    // 监听页码值改变事件
    handleCurrentChange(newPage) {
      this.queryInfo.page = newPage
      this.skillList()
    },
    async stateCut(row) {
      const res = await state(row)
      this.skillList()
      if(res.status == 200) return this.$message.success('更改状态成功')
    },
    handleClose() {

    },
    resetAddForm() {
      this.$refs.addFormRef.resetFields()
      this.addForm.title = ''
      this.addForm.text = ''
    },
    showAdded() {
      this.dialogVisible = true
    },
    async addArticle() {
      await add(this.addForm)

      this.dialogVisible = false
      this.skillList()
    }
  }
}
</script>

<style>
 .ql-editor {
   min-height: 200px
 }
</style>
